<template>
  <div id="question">
    <form action="/">
    <van-search show-action shape="round" @click-left-icon="onSelect" :clearable=false  v-model="groupId" @search="onSelect"  @cancel="onCancel" placeholder="请输入采购单号" />
    </form>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell-group
        v-for="item in list"
        :key="item.id"
      >
        <van-cell
          :title="item.group_id"
          border:true
          :label="item.buyer_name"
          @click.native="onCellClick($event, item)"
          class="van_cell"
        >
        <span class='create_class'>{{item.create_at}}</span>
          <!-- 使用 right-icon 插槽来自定义右侧图标 -->
          <template #right-icon>
            <van-button type="" size="mini" @click="PreViewRow(item)">任务</van-button>
          </template>
        </van-cell>

      </van-cell-group>
    </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import { purchaseList } from '@/api/purchase.js'
import { Search } from 'vant'
export default {
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      page: 1,
      limit: 20,
      groupId: ''
    }
  },
  methods: {
    async onLoad () {
      // 异步更新数据
      this.loading = true
      const res = await purchaseList(this.page, this.limit, this.groupId)
      if (res.data.code === 1) {
        // this.list = res.data.data
        this.page += 1
        // this.loading = false
      }
      const arr = res.data.data
      this.list = [...this.list, ...arr]
      this.loading = false
      if (arr.length === 0) {
        this.finished = true
      }
    },
    async onSelect (val) {
      // 异步更新数据
      this.loading = true
      const res = await purchaseList(1, 10, this.groupId)
      if (res.data.code === 1) {
        this.list = res.data.data
        this.finished = true
        this.loading = false
      }
    },
    onRefresh () {
      // 清空列表数据
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.loading = true
      this.onLoad()
      this.loading = false
      this.$toast.success('刷新成功！')
    },
    PreViewRow (item) {
      this.$router.push('/question/addWork?id=' + item.group_id + '&uid=' + item.system_id)
    },
    PreDetailsRow (item) {
      this.$router.push('/question/details?id=' + item.group_id)
    },
    onCancel () {
      this.groupId = ''
      this.page = 1
      this.limit = 20
      this.finished = false
      this.refreshing = false
      this.onLoad()
    },
    onCellClick (event, item) {
      // 判断点击的是否是title
      const titleElement = event.target.closest('.van-cell__title')
      if (titleElement) {
        console.log('Title clicked!')
        // 在这里处理title的点击事件
        this.$router.push('/question/details?id=' + item.group_id)
      }
    }
  }
}
</script>

<style>
 .create_class{
   margin-right:15px;
 }
</style>
